package com.sure.minion.services.base.utils;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import com.sure.minion.services.base.handler.DefaultDescription;
import com.sure.minion.services.base.handler.exception.ValidateAutoHandledException;
import com.sure.minion.services.base.vo.ValidateMsgVO;

/**
 * @Description 验证工具类
 * @author syuan
 * @date 2015年2月10日  下午4:55:08
 * Company: TravelSky
 */
public class ValidateUtil {
	
	/**
	 * @Description 验证
	 * @param request
	 * @author syuan
	 * @date 2015年2月10日  下午4:54:06
	 */
	public static <T> void validate(T requestObject){
		
		//先验证是否为空
		if(null == requestObject) {
			ValidateMsgVO validateMsgVO = new ValidateMsgVO("requestObject","不能为空",null);
			throw new ValidateAutoHandledException(DefaultDescription.BIZ_COMMON_VALIDATE_ERROR, validateMsgVO);
		}
		
		Validator validator = ValidatorSingleFactory.getValidator();
		Set<ConstraintViolation<T>> violations = validator.validate(requestObject); 
		if(!violations.isEmpty()) {
			throw new ValidateAutoHandledException(DefaultDescription.BIZ_COMMON_VALIDATE_ERROR, violations);
		}
	}

}
